package ar.com.photo_admin.domain

import grails.converters.*;
import org.springframework.dao.DataIntegrityViolationException


class EventAlbumController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

	def eventAlbumService;
	def eventService;
	
    def index() {
        redirect(action: "list", params: params)
    }

    def list(Integer max) {
        params.max = Math.min(max ?: 10, 100)
        [eventAlbumInstanceList: EventAlbum.list(params), eventAlbumInstanceTotal: EventAlbum.count()]
    }

    def create() {
        def eventAlbumInstance = new EventAlbum(params);		
		render(view: "show", model: [eventAlbumInstance: eventAlbumInstance, albumPhotos : [],event_id:params.event_id, unselectedPhotos : eventService.getEventPhotosAsJSON(params.event_id), editMode: true])
    }

    def save(Long id) {
				
		EventAlbum eventAlbumInstance = id!=null ? EventAlbum.get(id) : new EventAlbum(params);
								
		def selectedPhotos =  (params.selected_photos_id).split(",");		
		
		def photosList = []
		selectedPhotos.each {
			photosList.add(it);
		}
		
		eventAlbumInstance.properties = params;
				
		boolean savedCorrectly = eventAlbumService.saveAlbum(eventAlbumInstance,params.cover_photo_id,photosList,params.event_id)
		
		if (!savedCorrectly) {
			render(view: "show", model: [eventAlbumInstance: eventAlbumInstance])
			return
		}
		 		
		flash.message = "El album ha sido guardado"
		redirect(action: "show", id: eventAlbumInstance.id)
    }

    def show(Long id) {
		EventAlbum eventAlbumInstance = EventAlbum.get(id) ;
		
        if (!eventAlbumInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'eventAlbum.label', default: 'EventAlbum'), id])
            redirect(action: "list")
            return
        }

        [eventAlbumInstance: eventAlbumInstance, albumPhotos : eventAlbumService.getAlbumPhotosAsJSON(id),event_id:eventAlbumInstance.event.id, editMode:false]
				
    }
	
	def edit() {
		
		def eventId = params.eventId;
		def albumId = params.albumId;
		log.info "album"+ albumId+".Event: "+eventId;
		def possiblePhotos = null;
		if (albumId != null && albumId !="")
		{			
			log.info "Editando el album" + params.albumId;
			def eventAlbumInstance = EventAlbum.get(params.albumId)
			
			if (!eventAlbumInstance) {
				flash.message = message(code: 'default.not.found.message', args: [message(code: 'eventAlbum.label', default: 'EventAlbum'), albumId])
				redirect(action: "list")
				return
			}
			possiblePhotos = eventAlbumService.getNotInAlbumButPossiblePhotosAsJSON(params.albumId);
			
		}		
		else {
			possiblePhotos = eventService.getEventPhotosAsJSON(eventId);
		}		
		log.info possiblePhotos;
		render(possiblePhotos);		
		return				
	}
/*
    def update(Long id, Long version) {
        def eventAlbumInstance = EventAlbum.get(id)
        if (!eventAlbumInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'eventAlbum.label', default: 'EventAlbum'), id])
            redirect(action: "list")
            return
        }

        if (version != null) {
            if (eventAlbumInstance.version > version) {
                eventAlbumInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
                          [message(code: 'eventAlbum.label', default: 'EventAlbum')] as Object[],
                          "Another user has updated this EventAlbum while you were editing")
                render(view: "edit", model: [eventAlbumInstance: eventAlbumInstance])
                return
            }
        }

        eventAlbumInstance.properties = params

        if (!eventAlbumInstance.save(flush: true)) {
            render(view: "edit", model: [eventAlbumInstance: eventAlbumInstance])
            return
        }

        flash.message = message(code: 'default.updated.message', args: [message(code: 'eventAlbum.label', default: 'EventAlbum'), eventAlbumInstance.id])
        redirect(action: "show", id: eventAlbumInstance.id)
    }

    def delete(Long id) {
        def eventAlbumInstance = EventAlbum.get(id)
        if (!eventAlbumInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'eventAlbum.label', default: 'EventAlbum'), id])
            redirect(action: "list")
            return
        }

        try {
            eventAlbumInstance.delete(flush: true)
            flash.message = message(code: 'default.deleted.message', args: [message(code: 'eventAlbum.label', default: 'EventAlbum'), id])
            redirect(action: "list")
        }
        catch (DataIntegrityViolationException e) {
            flash.message = message(code: 'default.not.deleted.message', args: [message(code: 'eventAlbum.label', default: 'EventAlbum'), id])
            redirect(action: "show", id: id)
        }
    }*/
}
